//
//  LoginVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/7.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit
import AdSupport

class LoginVc: TTBaseVC {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 微信登录通知
        NotificationCenter.default.addObserver(self,selector: #selector(WXLoginSuccess(notice:)),name: NSNotification.Name(rawValue: "WXLoginSuccessNotification"),object: nil)
        
        let image = UIImageView.init(frame: FRAME_CGFloat(SCREEN_WIDTH / 2 - 32, 127, 64, 108.5))
        image.image = IMAGE(name: "logoShadow")
        image.layer.cornerRadius = 10
        image.layer.masksToBounds = true
        self.view.addSubview(image)
        
        let butn = UIButton.init(type: .custom)
        butn.frame = FRAME_CGFloat(37.5, 265, SCREEN_WIDTH - 75, 44)
        butn.setBackgroundImage(IMAGE(name: "Rectangle"), for: .normal)
        butn.setTitle("微信一键登录", for: .normal)
        butn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        butn.addTarget(self, action: #selector(wxLoginBtnAction), for: .touchUpInside)
        self.view.addSubview(butn)
        
        let backBtn = UIButton.init(type: .custom)
        backBtn.frame = FRAME_CGFloat(10, OFFSET_TOP - 30, 16, 26)
        backBtn.setImage(IMAGE(name: "back"), for: .normal)
        backBtn.addTarget(self, action: #selector(backBtnAction), for: .touchUpInside)
        self.view.addSubview(backBtn)
        
        self.view.backgroundColor = WhiteBackColor
    }
    
    @objc func wxLoginBtnAction() {
        if WXApi.isWXAppInstalled() {
            let req = SendAuthReq.init()
            req.scope = "snsapi_userinfo"
            req.state = "APP"
            WXApi.send(req)
        }else{
            
        }
    }
    
    @objc func backBtnAction() {
        self.dismiss(animated: true) {
            
        }
    }
    
    //MARK:成功后其它控制器的通知方法
    @objc func WXLoginSuccess(notice:Notification) {
        print((notice.userInfo!["str"])!)
        let str = notice.userInfo!["str"] as! String
        
        MineRequestModel.WX_Logion(str: str) { (success, message, model) in
            if success && model != nil {
                UserDefaults.standard.set(model?.id, forKey: UserId)
                UserDefaults.standard.set(model?.wxNickname, forKey: WxNickname)
                UserDefaults.standard.set(model?.wxAvatarUrl, forKey: WxAvatarUrl)
                self.put()
                if model?.bandMobile == 0 {
                    let vc = BindingPhoneVc()
                    PUSHVIEWCONTROLLER(vc)
                    self.dismiss(animated: true, completion: {
                        
                    })
                } else {
                    self.dismiss(animated: true, completion: nil)
                }
                
            } else {
                TTAlertView.showMessage(message: "授权失败，请重新登录", second: 2)
            }
        }
    }
    
    func put() {
        let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
        HomeRequestModel.putIDFA(parameters: ["platType":"ios","deviceNumber":idfa,"userId":String.getUserId()])
    }
}
